package com.weiyi.mashangzhu.Weight;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Weight.calendartest.MonthTimeAdapter;
import com.weiyi.mashangzhu.Weight.calendartest.entity.DayTimeEntity;
import com.weiyi.mashangzhu.Weight.calendartest.entity.MonthTimeEntity;
import com.weiyi.mashangzhu.Weight.calendartest.entity.UpdataCalendar;
import com.weiyi.mashangzhu.Weight.calendartest.utils.SharePreferences;

import java.util.ArrayList;
import java.util.Calendar;

import de.greenrobot.event.EventBus;

/**
 * Created by win7 on 2017/9/28.
 */

public class PopWindow_Date {
    private TextView startTime;          //开始时间
    private TextView stopTime;           //结束时间

    private RecyclerView reycycler;
    private MonthTimeAdapter adapter;
    private ArrayList<MonthTimeEntity> datas;


    public static DayTimeEntity startDay;
    public static DayTimeEntity stopDay;

    SharePreferences isPreferences;
    private void initData(final PopupWindow popupWindow,Context context) {
        startDay = new DayTimeEntity(0, 0, 0, 0);
        stopDay = new DayTimeEntity(-1, -1, -1, -1);
        datas = new ArrayList<>();

        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;

        datas.add(new MonthTimeEntity(year, month));                //

        for (int i = 0; i < 6; i++) {
            MonthTimeEntity monthTimeEntity = new MonthTimeEntity();
            if (month >= 12) {//明年
                year = c.get(Calendar.YEAR) + 1;
                month = 1;
                monthTimeEntity.setYear(year);
                monthTimeEntity.setMonth(month);
            } else {
                month = month + 1;
                monthTimeEntity.setYear(year);
                monthTimeEntity.setMonth(month);
            }

            datas.add(monthTimeEntity);

        }

        adapter = new MonthTimeAdapter(datas, context);
        reycycler.setAdapter(adapter);

        adapter.setOnDayItemClickListener(new MonthTimeAdapter.OnDayItemClickListener() {
            @Override
            public void OnDayItemClick(View view, int position) {
                isPreferences.updateSp("start_month_position", startDay.getMonthPosition());
                isPreferences.updateSp("start_day_position", startDay.getDayPosition());
                isPreferences.updateSp("end_month_position", stopDay.getMonthPosition());
                isPreferences.updateSp("end_day_position", stopDay.getDayPosition());

                isPreferences.updateSp("start_year", startDay.getYear());
                isPreferences.updateSp("start_month", startDay.getMonth());
                isPreferences.updateSp("start_day", startDay.getDay());

                isPreferences.updateSp("end_year", stopDay.getYear());
                isPreferences.updateSp("end_month", stopDay.getMonth());
                isPreferences.updateSp("end_day", stopDay.getDay());


                Intent i = new Intent();
                i.putExtra("start_year", startDay.getYear());
                i.putExtra("start_month", startDay.getMonth());
                i.putExtra("start_day", startDay.getDay());

                i.putExtra("end_year", stopDay.getYear());
                i.putExtra("end_month", stopDay.getMonth());
                i.putExtra("end_day", stopDay.getDay());
//                setResult(RESULT_OK, i);
//                searchDate.setText(startDay.getYear()+"-"+startDay.getMonth()+"-"+startDay.getDay()+"/"+stopDay.getYear()+"-"+stopDay.getMonth()+"-"+stopDay.getDay());
                popupWindow.dismiss();

            }
        });

    }

    public void showPopupWindow(Context context) {
        isPreferences = new SharePreferences(context);
        EventBus.getDefault().register(this);
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_win_date, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);

        startTime = (TextView) contentView.findViewById(R.id.plan_time_txt_start);
        stopTime = (TextView) contentView.findViewById(R.id.plan_time_txt_stop);

        reycycler = (RecyclerView) contentView.findViewById(R.id.plan_time_calender);
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(context,   // 上下文
                        LinearLayout.VERTICAL,  //垂直布局,
                        false);

        reycycler.setLayoutManager(layoutManager);

        MoveToPosition(layoutManager, reycycler, isPreferences.getSp().getInt("start_month_position", 0));


        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.popup_window_bottom_animation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));
        // 设置好参数之后再show
        popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);
        initData(popupWindow,context);

    }

    //直接显示第n个item内容
    public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {
        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        if (n <= firstItem) {
            mRecyclerView.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = mRecyclerView.getChildAt(n - firstItem).getTop();
            mRecyclerView.scrollBy(0, top);
        } else {
            mRecyclerView.scrollToPosition(n);
        }

    }


    public void onEventMainThread(UpdataCalendar event) {
        adapter.notifyDataSetChanged();
        startTime.setText(startDay.getMonth() + "月" + startDay.getDay() + "日" + "\n");
        if (stopDay.getDay() == -1) {
            stopTime.setText("结束" + "\n" + "时间");
        } else {
            stopTime.setText(stopDay.getMonth() + "月" + stopDay.getDay() + "日" + "\n");
        }
    }
}
